1
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
2
|
|
|
import { Inject } from '@nestjs/common'; |
3
|
|
|
import { UpdateEventCommand } from './UpdateEventCommand'; |
4
|
|
|
import { ITaskRepository } from 'src/Domain/Task/Repository/ITaskRepository'; |
5
|
|
|
import { IProjectRepository } from 'src/Domain/Project/Repository/IProjectRepository'; |
6
|
|
|
import { IEventRepository } from 'src/Domain/FairCalendar/Repository/IEventRepository'; |
7
|
|
|
import { EventType } from 'src/Domain/FairCalendar/Event.entity'; |
8
|
|
|
import { MaximumEventReachedException } from 'src/Domain/FairCalendar/Exception/MaximumEventReachedException'; |
9
|
|
|
import { ProjectOrTaskMissingException } from 'src/Domain/FairCalendar/Exception/ProjectOrTaskMissingException'; |
10
|
|
|
import { EventNotFoundException } from 'src/Domain/FairCalendar/Exception/EventNotFoundException'; |
11
|
|
|
import { EventDoesntBelongToUserException } from 'src/Domain/FairCalendar/Exception/EventDoesntBelongToUserException'; |
12
|
|
|
import { DoesEventBelongToUser } from 'src/Domain/FairCalendar/Specification/DoesEventBelongToUser'; |
13
|
|
|
import { AbstractProjectAndTaskGetter } from './AbstractProjectAndTaskGetter'; |
14
|
|
|
import { IsMaximumTimeSpentReached } from 'src/Domain/FairCalendar/Specification/IsMaximumTimeSpentReached'; |
15
|
|
|
|
16
|
|
|
@CommandHandler(UpdateEventCommand) |
17
|
|
|
export class UpdateEventCommandHandler extends AbstractProjectAndTaskGetter { |
18
|
|
|
constructor( |
19
|
|
|
@Inject('ITaskRepository') taskRepository: ITaskRepository, |
20
|
|
|
@Inject('IProjectRepository') projectRepository: IProjectRepository, |
21
|
|
|
@Inject('IEventRepository') |
22
|
|
|
private readonly eventRepository: IEventRepository, |
23
|
|
|
private readonly doesEventBelongToUser: DoesEventBelongToUser, |
24
|
|
|
private readonly isMaximumTimeSpentReached: IsMaximumTimeSpentReached |
25
|
|
|
) { |
26
|
|
|
super(taskRepository, projectRepository); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public async execute(command: UpdateEventCommand): Promise<string> { |
30
|
|
|
const { id, type, projectId, taskId, summary, time, user } = command; |
31
|
|
|
|
32
|
|
|
const event = await this.eventRepository.findOneById(id); |
33
|
|
|
if (!event) { |
34
|
|
|
throw new EventNotFoundException(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (false === this.doesEventBelongToUser.isSatisfiedBy(event, user)) { |
38
|
|
|
throw new EventDoesntBelongToUserException(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (type === EventType.MISSION && (!projectId || !taskId)) { |
42
|
|
|
throw new ProjectOrTaskMissingException(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
const [project, task] = await Promise.all([ |
46
|
|
|
this.getProject(projectId), |
47
|
|
|
this.getTask(taskId) |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
if ( |
51
|
|
|
true === (await this.isMaximumTimeSpentReached.isSatisfiedBy(event, time)) |
52
|
|
|
) { |
53
|
|
|
throw new MaximumEventReachedException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
event.update(type, time, project, task, summary); |
57
|
|
|
await this.eventRepository.save(event); |
58
|
|
|
|
59
|
|
|
return event.getId(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|